package com.poplar.t;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 泛型擦除
 * @author: ztd
 * @date 2020/3/18 下午6:59
 */
public class GenericTest {
    public static void main(String[] args) {
//        classTest();
        addByReflection();
    }

    public static void classTest() {
        List<String> strings = new ArrayList<>();
        strings.add("123");
        List<Integer> integers = new ArrayList<>();
        integers.add(1);
        System.out.println(strings.getClass() == integers.getClass());
    }

    public static void addByReflection() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);

        try {
            list.getClass().getMethod("add", Object.class).invoke(list, "asdf");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

}
